package com.huirui.server.controller.mini.program;

import com.huirui.server.pojo.result.PageResult;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.*;
import com.huirui.server.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/category")
@Slf4j
@Tag(name = "分类管理", description = "微信小程序分类查询相关")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("/top")
    @Operation(summary = "查询一级分类")
    public Result<List<TopCategoryVo>> listTopCategories() {
        List<TopCategoryVo> categoryVoList = categoryService.listTopCategories();
        return Result.success(categoryVoList);
    }

    @GetMapping("/secondary")
    @Operation(summary = "查询二级分类")
    public Result<List<SecondaryCategoryVo>> listSecondaryCategories(@RequestParam Long id) {
        List<SecondaryCategoryVo> categoryVoList = categoryService.listSecondaryCategories(id);
        return Result.success(categoryVoList);
    }

    @GetMapping("/tertiary")
    @Operation(summary = "查询三级分类")
    public Result<List<TertiaryCategoryVo>> listTertiaryCategories(@RequestParam Long id) {
        List<TertiaryCategoryVo> categoryVoList = categoryService.listTertiaryCategories(id);
        return Result.success(categoryVoList);
    }

    @GetMapping("/show")
    @Operation(summary = "获取携带三级分类的二级分类数据")
    public Result<List<CategoryAnotherVo>> getPopupCategoryData(@RequestParam Long id) {
        List<CategoryAnotherVo> list = categoryService.getPopupCategoryData(id);
        return Result.success(list);
    }

    @GetMapping("/list/product")
    @Operation(summary = "根据分类ID获取商品列表")
    public Result<PageResult<ProductBriefInfoVo>> listProductListByCategoryId(
            @RequestParam("categoryId") Long categoryId,
            @Parameter(description = "排序方式 0-根据销量排序，1-根据价格排序")
            @RequestParam("orderBy") Integer orderBy,
            @Parameter(description = "价格升序或降序 0-价格升序，1-价格降序")
            @RequestParam(value = "priceOrder") Integer priceOrder,
            @RequestParam("page") Long page,
            @RequestParam("pageSize") Long pageSize
    ) {
        PageResult<ProductBriefInfoVo> res = categoryService.listProductListByCategoryId(categoryId, orderBy, priceOrder, page, pageSize);
        return Result.success(res);
    }


}
